﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 列出文件夹文件
{
    public class viewModel : INotifyPropertyChanged
    {
        private viewModel()
        {
            _model = new model();
        }

        public static viewModel g_viewModel()
        {

            if (_viewModel == null)
            {
                _viewModel = new viewModel();
            }
            return _viewModel;
        }
        public string 要列出的文件夹
        {
            set
            {
                _model.要列出的文件夹 = value;
                OnPropertyChanged("要列出的文件夹");
            }
            get
            {
                return _model.要列出的文件夹;
            }
        }
        public string 要输出的文件
        {
            set
            {
                _model.要输出的文件 = value;
                OnPropertyChanged("要输出的文件");
            }
            get
            {
                return _model.要输出的文件;
            }
        }
        public string reminder
        {
            set
            {
                _reminder.Clear();
                _reminder.Append(value);
                OnPropertyChanged("reminder");
            }
            get
            {
                return _reminder.ToString();
            }
        }
        public void 列出路径()
        {
            _model.列出路径();
            reminder = "列完整路径成功 " + 要输出的文件;
        }
        public void 列树文件()
        {
            _model.列树文件();
            reminder = "列树文件成功 " + 要输出的文件;
        }        
        public void open_file()
        {
            System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
            folder.ShowDialog();
            if (folder.SelectedPath != string.Empty)
            {
                要列出的文件夹 = folder.SelectedPath;
            }

        }
        public void save_file()
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            string temp=要列出的文件夹.Substring(要列出的文件夹.LastIndexOf("\\")+1 , 要列出的文件夹.Length - 要列出的文件夹.LastIndexOf("\\")-1);
            dlg.FileName = temp;
            dlg.DefaultExt = ".txt";
            dlg.Filter = "data(.txt)|*.*";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                //代码
                要输出的文件 = dlg.FileName;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private model _model;
        private StringBuilder _reminder=new StringBuilder();
        private static viewModel _viewModel=new viewModel();
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this , new PropertyChangedEventArgs(name));
            }
        }
    }
}
